<?php 
//update_option('siteurl','http://localhost:8080/xpct');update_option('home','http://localhost:8080/xpct');

function t($data,$exit=1) {
	echo '<pre style="text-align:left;">';
	print_r($data);
	echo '</pre>';
	if($exit)exit;
}
function get_first_image_src($content,$type='thumb'){  
  $matches=array();
  
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
  $first_img = $matches [1] [0];
  return $first_img;
}	
function register_script(){
	wp_enqueue_script('superfish',get_template_directory_uri().'/js/superfish.js',array('jquery'));
	wp_enqueue_script('jquery.flexslider-min',get_template_directory_uri().'/js/jquery.flexslider-min.js',array('jquery'));
	wp_enqueue_script('jquery.kwicks-1.5.1',get_template_directory_uri().'/js/jquery.kwicks-1.5.1.js',array('jquery'));
	wp_enqueue_script('jquery.easing.1.3',get_template_directory_uri().'/js/jquery.easing.1.3.js',array('jquery'));
	wp_enqueue_script('touchTouch.jquery',get_template_directory_uri().'/js/touchTouch.jquery.js',array('jquery'));
	wp_enqueue_script('bootstrap',get_template_directory_uri().'/js/bootstrap.js',array('jquery'),'1',true);
	wp_enqueue_script('mycommon',get_template_directory_uri().'/js/mycommon.js');
	if(is_single()){
		wp_enqueue_script('videojs',get_template_directory_uri().'/js/video.js',array('jquery'),'1',true);
		wp_enqueue_script('youtubejs',get_template_directory_uri().'/js/youtube.js',array('jquery','videojs'),'1',true);
		wp_enqueue_style('videojs',get_template_directory_uri().'/css/video-js.min.css');
		
		wp_enqueue_script('xpctvideo',get_template_directory_uri().'/js/xpctvideo.js',array('jquery','videojs'),'1',true);
	}
}

function home_style(){
	//wp_enqueue_style('home',get_template_directory_uri() . '/css/home.css',array());
}
//=================== excerpt setting =========
function custom_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function new_excerpt_more( $more ) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
//================ tinymce config ===============
function mytheme_tinymce_config( $init ) {
 $valid_iframe = 'iframe[id|class|title|style|align|frameborder|height|longdesc|marginheight|marginwidth|name|scrolling|src|width]';
 if ( isset( $init['extended_valid_elements'] ) ) {
  $init['extended_valid_elements'] .= ',' . $valid_iframe;
 } else {
  $init['extended_valid_elements'] = $valid_iframe;
 }
 return $init;
}
add_filter('tiny_mce_before_init', 'mytheme_tinymce_config');

add_action( 'wp_enqueue_script', 'register_script' );
add_theme_support( 'post-thumbnails' );

//========add custom field in admin post:
add_filter('manage_edit-post_columns', 'my_extra_cake_columns');  
function my_extra_cake_columns($columns) {      
	$columns['release'] =__('Release','myplugindomain');    
	$columns['embed'] =__('Video','myplugindomain');     
	$columns['download url'] =__('Link','myplugindomain');      
    return $columns;  
}  


function yoast_change_opengraph_type( $type ) {
  if ( is_single() )
    return 'video';
}
add_filter( 'wpseo_opengraph_type', 'yoast_change_opengraph_type', 10, 1 );

add_action( 'manage_posts_custom_column', 'my_cake_column_content', 10, 2 );  
function my_cake_column_content( $column_name, $post_id ) {  
    if ( 'release' == $column_name )  {
    	 $slices = date('M d Y',strtotime(get_post_meta($post_id, 'release', true)));
    	echo $slices;  
    }
    else if ( 'embed' == $column_name )  {
    	 $slices = get_post_meta($post_id, 'embed', true);
    	echo $slices;  
    }
	else if ( 'download url' == $column_name )  {
    	 $slices = get_post_meta($post_id, 'download url', true);
    	echo $slices;  
    }
        return;  
    //Get number of slices from post meta  
   
}  

add_filter( 'manage_edit-post_sortable_columns', 'my_sortable_cake_column' );  
function my_sortable_cake_column( $columns ) {
		
    $columns['release'] = 'release';    
    $columns['embed'] = 'embed';  
    $columns['link'] = 'link';    
    //To make a column 'un-sortable' remove it from the array  
    //unset($columns['date']);  
  
    return $columns;  
} 


function wpse_55791_custom_orderby($orderby)
{
    global $wp_query, $wpdb;

    // check for order by custom_order
    if ($wp_query->get('meta_key') == 'release')
    {
        $orderby = "{$wpdb->postmeta}.meta_value='', ".$orderby;
    }
    return $orderby;
}
//add_filter('posts_orderby', 'wpse_55791_custom_orderby', 10, 1);

add_action( 'pre_get_posts', 'my_slice_orderby' );  
function my_slice_orderby( $query ) {  
    if( ! is_admin() )  
        return;  
  
    $orderby = $query->get( 'orderby');  
  
    if( 'release' == $orderby ) {  
        $query->set('meta_key','release');  
        $query->set('orderby','meta_value');  
    }  
    else if( 'embed' == $orderby ) {  
        $query->set('meta_key','embed');  
        $query->set('orderby','meta_value');  
    }  
    else if( 'link' == $orderby ) {  
        $query->set('meta_key','link');  
        $query->set('orderby','meta_value');  
    }  
}  
//========================================
?>